//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package alarm_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_alarm_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"google.golang.org/protobuf/proto"
)

const (
	ALARM_LEVEL_HIT   = 0
	ALARM_LEVEL_ALERT = 1
)

type AlarmDbItem struct {
	AlarmId    string `bson:"_id"`
	ProjectId  string `bson:"projectId"`
	TimeStamp  int64  `bson:"timeStamp"`
	AlarmType  uint32 `bson:"alarmType"`
	AlarmLevel uint32 `bson:"alarmLevel"`
	Content    []byte `bson:"content"`
}

func (item *AlarmDbItem) ToAlarm() (*project_alarm_api.Alarm, error) {
	retItem := &project_alarm_api.Alarm{}
	err := proto.Unmarshal(item.Content, retItem)
	return retItem, err
}

type _AlarmDao struct {
	mc *mongo.Collection
}

func newAlarmDao(db *mongo.Database) (*_AlarmDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_ALARM + "alarm")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"timeStamp", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_AlarmDao{mc: mc}, nil
}

func (db *_AlarmDao) Insert(ctx context.Context, dbItem *AlarmDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_AlarmDao) Remove(ctx context.Context, projectId, alarmId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": alarmId, "projectId": projectId})
	return err
}

func (db *_AlarmDao) RemoveByProject(ctx context.Context, projectId string, alarmType uint32) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "alarmType": alarmType})
	return err
}

func (db *_AlarmDao) Exist(ctx context.Context, projectId, alarmId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": alarmId, "projectId": projectId})
	return count > 0, err
}

func (db *_AlarmDao) Get(ctx context.Context, projectId, alarmId string) (*AlarmDbItem, error) {
	retItem := &AlarmDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": alarmId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_AlarmDao) Count(ctx context.Context, projectId string, filterByLevel bool, alarmLevel uint32) (uint32, error) {
	query := bson.M{"projectId": projectId}
	if filterByLevel {
		query["alarmLevel"] = alarmLevel
	}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_AlarmDao) List(ctx context.Context, projectId string, filterByLevel bool, alarmLevel uint32, offset, limit int64) ([]*AlarmDbItem, error) {
	query := bson.M{"projectId": projectId}
	if filterByLevel {
		query["alarmLevel"] = alarmLevel
	}
	cursor, err := db.mc.Find(ctx, query,
		options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*AlarmDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}
